class Solution:
    """
    128. 最长连续序列
    https://leetcode-cn.com/problems/longest-consecutive-sequence/
    @date 2021年1月15日20:00:58
    @author mizzle rain
    """

    def longestConsecutive(self, nums):
        """
        时间复杂度不是O(n)，不推荐此做法
        """
        nums = list(set(nums))
        nums.sort()
        start, res = 0,  1
        n = len(nums)
        if n == 0:
            return 0
        for i in range(1, n):
            if nums[i] != nums[i-1] + 1:
                res = max(res, i-start)
                start = i
        res = max(res, n-start)
        return res


if __name__ == "__main__":
    s = Solution()
    res = s.longestConsecutive([1,2,0,1])
    print(res)
